﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlarmClock
{
    public delegate void ClockEvent(object sender);
    class Clock
    {
        private int hour;
        private int minute;
        private int second; //表示当前时钟时间

        private int hour1;
        private int minute1;
        private int second1;//表示闹钟时间
        public event ClockEvent Tick;
        public event ClockEvent Alarm;
        public int Hour
        {
            get { return hour; }
            set { hour = value; }
        }
        public int Minute
        {
            get { return minute; }
            set { minute = value;}
        }
        public int Second
        {
            get { return second; }
            set { second = value; }
        }
        public Clock(int hour,int minute,int second)  //时钟构造方法
        {
            if (hour > 60 || minute > 60 || second > 60)
                throw new Exception("输入错误");
            else
            {
                this.hour = hour;
                this.minute = minute;
                this.second = second;
            }
        }
        public Clock()
        {
            hour = minute = second = 0;

        }
        public void SetTime(int hour,int minute,int second)
        {
            if (hour > 60 || minute > 60 || second > 60)
                throw new Exception("输入错误");
            else
            {
                this.hour = hour;
                this.minute = minute;
                this.second = second;
            }
        }
        public bool IsAlarm()
        {
            if (hour == hour1 && minute == minute1 && second == second1)
            {
                Alarm(this);
                return true;
            }
            else
                return false;
        }
        public void Clock_start() //模拟时钟走时
        {
            if (IsAlarm())
                return;
            else
            {
                while (true)
                {
                    this.Show();
                    Tick(this);
                    second++;
                    if (second == 60)
                    {
                        second = 0;
                        minute++;
                    }
                    if (minute == 60)
                    {
                        minute = 0;
                        hour++;
                    }
                    if (hour == 24)
                    {
                        hour = 0;
                    }
                    if (IsAlarm()) //判断是否闹钟响了
                        return;
                    
                }
            }
                
        }
        public void Show()
        {
            Console.WriteLine($"{hour}:{minute}:{second}");
        }
        public void SetAlarm(int hour,int minute,int second)
        {
            hour1 = hour;
            minute1 = minute;
            second1 = second;
        }

    }
}
